﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using DentScan2.DB;

namespace DentScan2.WebApp.Classes
{
    public class BaseForm : System.Web.UI.Page
    {

        protected DLDataContext dl = new DLDataContext(ConfigurationManager.ConnectionStrings["DBCONN"].ConnectionString);

        public Guid? UserId
        {
            get
            {
                return (Guid?)Session["UserId"];

            }
            set
            {
                Session["UserId"] = value;
            }
        }

        public int? Level
        {
            get
            {
                return (int?)Session["Level"];
            }
            set
            {
                Session["Level"] = value;
            }
        }



        public Guid? AreaId
        {
            get
            {
                return (Guid?)Session["AreaId"];

            }
            set
            {
                Session["AreaId"] = value;
            }
        }

        public String UserName
        {
            get
            {
                return Session["UserName"].ToString();

            }
            set
            {
                Session["UserName"] = value;
            }
        }

        public String FullName
        {
            get
            {
                return Session["FullName"].ToString();

            }
            set
            {
                Session["FullName"] = value;
            }
        }

        public String Phone
        {
            get
            {
                return Session["Phone"] == null ? "" : Session["Phone"].ToString();

            }
            set
            {
                Session["Phone"] = value;
            }
        }


        public Classes.Damage Damage
        {
            get
            {
                return Session["Damage"] as Classes.Damage;
            }
            set
            {
                Session["Damage"] = value;
            }

        }

        

        protected void CheckLogged()
        {
            if (Session["UserId"] == null)
                Response.Redirect("logout.aspx", true);
        }

        protected void CheckLevel(int level)
        {
            if ((int)Session["Level"] != level)
                Response.Redirect("logout.aspx");
        }


        protected void Alert(string msg)
        {
            StringBuilder scriptBuffer = new StringBuilder();

            scriptBuffer.Append("<script language='JavaScript'>");
            scriptBuffer.Append("alert('" + msg + "');");
            scriptBuffer.Append("<");
            scriptBuffer.Append("/");
            scriptBuffer.Append("script");
            scriptBuffer.Append(">");
            Type j = this.GetType();
            System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, j, "OK", scriptBuffer.ToString(), false);
        }

        public BaseForm()
        {
            this.Load += new EventHandler(this.PageLoad);
        }


        protected void PageLoad(object sender, EventArgs e)
        {
            DesignMenu();
        }


        private void DesignMenu()
        {
            if (this.Master == null)
                return;
            Menu menu = (this.Master as DentScan2.WebApp.Site).Menu;
            if (this.UserId == null)
                menu.Visible = false;
            else
            {
                switch (this.Level)
                {
                    case 2:
                        menu.Items[1].ChildItems.RemoveAt(0);//Редакция на потребители
                        menu.Items[2].ChildItems.RemoveAt(1);//Страни
                        break;
                }

            }

        }



    }
}


